﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using PPT = Microsoft.Office.Interop.PowerPoint;
using System.Runtime.Serialization.Json;
using System.Runtime.InteropServices;
using System.Net.Sockets;
using System.Threading;
using System.Net;
using System.Net.NetworkInformation;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace VoiceControlDesktopClient
{
    public partial class Form1 : Form
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);
        const uint WM_APPCOMMAND = 0x319;
        const uint APPCOMMAND_VOLUME_UP = 0x0a;
        const uint APPCOMMAND_VOLUME_DOWN = 0x09;
        const uint APPCOMMAND_VOLUME_MUTE = 0x08;

       

        PPT.Application pptApp;
        public Form1()
        {
            InitializeComponent();
            this.Load += Form1_Load;
        }

        void Form1_Load(object sender, EventArgs e)
        {
            if (mySocket == null)
            {
                mySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            }
            if (MyEvent == null)
            {
                MyEvent = new ManualResetEvent(false);
            }  
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //ProcessStartInfo Info = new ProcessStartInfo();
            
            //Process.Start("http://baidu.com");
           // CloseSound();
            
        }

        //private void PPTOpen(string Path)
        //{
        //    pptApp = new PPT.Application();
        //    pptApp.Visible = true;
        //    pptApp.WindowState = PPT.PpWindowState.ppWindowNormal;

        //    //PPT.SlideShowSettings setting=new pp;
        //    setting.StartingSlide = 1;
        //    setting.EndingSlide = 5;
        //}


        void Add()
        {
            SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_UP * 0x10000);
        }
        void Decrease()
        {
            SendMessage(this.Handle, WM_APPCOMMAND, 0x30292, APPCOMMAND_VOLUME_DOWN * 0x10000);  
        }

        void CloseSound()
        {
            SendMessage(this.Handle, WM_APPCOMMAND, 0x200eb0, APPCOMMAND_VOLUME_MUTE * 0x10000);   
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //Add();
            GetDis();
            
        }

        public const uint WM_SYSCOMMAND = 0x0112;
        public const uint SC_MONITORPOWER = 0xF170;
        [DllImport("user32")]
        public static extern IntPtr SendMessage(IntPtr hWnd, uint wMsg, uint wParam, int lParam);

        public enum DMDO
        {
            DEFAULT = 0,
            D90 = 1,
            D180 = 2,
            D270 = 3
        }

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        struct DEVMODE
        {
            public const int DM_DISPLAYFREQUENCY = 0x400000;
            public const int DM_PELSWIDTH = 0x80000;
            public const int DM_PELSHEIGHT = 0x100000;
            public const int DM_BITSPERPEL = 262144;
            private const int CCHDEVICENAME = 32;
            private const int CCHFORMNAME = 32;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
            public string dmDeviceName;
            public short dmSpecVersion;
            public short dmDriverVersion;
            public short dmSize;
            public short dmDriverExtra;
            public int dmFields;
            public int dmPositionX;
            public int dmPositionY;
            public DMDO dmDisplayOrientation;
            public int dmDisplayFixedOutput;
            public short dmColor;
            public short dmDuplex;
            public short dmYResolution;
            public short dmTTOption;
            public short dmCollate;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHFORMNAME)]
            public string dmFormName;
            public short dmLogPixels;
            public int dmBitsPerPel;
            public int dmPelsWidth;
            public int dmPelsHeight;
            public int dmDisplayFlags;
            public int dmDisplayFrequency;
            public int dmICMMethod;
            public int dmICMIntent;
            public int dmMediaType;
            public int dmDitherType;
            public int dmReserved1;
            public int dmReserved2;
            public int dmPanningWidth;
            public int dmPanningHeight;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        static extern int ChangeDisplaySettings([In] ref DEVMODE lpDevMode, int dwFlags);
        int dWidth = 0;
        int dHeight = 0;
        private void GetDis()//获取分辨率
        {
            Size s = SystemInformation.PrimaryMonitorSize;
            dWidth = s.Width;
            dHeight = s.Height;
            MessageBox.Show(dWidth.ToString() + " × " + dHeight.ToString() + " 像素");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            long RetVal = 0;
            DEVMODE dm = new DEVMODE();
            dm.dmSize = (short)Marshal.SizeOf(typeof(DEVMODE));
            dm.dmPelsWidth = 1024;//宽
            dm.dmPelsHeight = 768;//高
            dm.dmDisplayFrequency = 60;//刷新率
            dm.dmFields = DEVMODE.DM_PELSWIDTH | DEVMODE.DM_PELSHEIGHT | DEVMODE.DM_DISPLAYFREQUENCY | DEVMODE.DM_BITSPERPEL;
            RetVal = ChangeDisplaySettings(ref dm, 0);
        }

        Socket mySocket = null;
        ManualResetEvent MyEvent = null;  
        private void button4_Click(object sender, EventArgs e)
        {
            //IPHostEntry ipe = Dns.GetHostEntry(Dns.GetHostName());
            //IPAddress ipa = ipe.AddressList[0];
            if (mySocket != null)
            {
                SocketAsyncEventArgs connArg = new SocketAsyncEventArgs();
                // 要连接的远程服务器  
                connArg.RemoteEndPoint = new DnsEndPoint("192.168.1.64", 8080);
                // 操作完成后的回调  
                connArg.Completed += (sendObj, arg) =>
                {
                    if (arg.SocketError == SocketError.Success) //连接成功  
                    {
                        MessageBox.Show("Success.");
                        UserInfo info = new UserInfo
                        {
                            ip = "192.168.1.64",
                            name = "yy",
                            password = "123456"
                        };
                        string content = "Login#" + JsonConvert.SerializeObject(info);
                        SendCommand(content);
                    }
                    else
                    {
                        //Dispatcher.BeginInvoke(() =>
                        //{
                        //    txtbInfo.Text = "连接失败，错误：" + arg.SocketError.ToString();
                        //});
                    }
                    // 向调用线程报告操作结束  
                    MyEvent.Set();
                };
                // 重置线程等待事件  
                MyEvent.Reset();
                //txtbInfo.Text = "正在连接，请等候……";
                // 开始异连接  
                mySocket.ConnectAsync(connArg);
                // 等待连接完成  
                MyEvent.WaitOne(6000);
            }  
        }

        private void SendCommand(string txt)
        {
            if (mySocket != null && mySocket.Connected)
            {
                SocketAsyncEventArgs sendArg = new SocketAsyncEventArgs();
                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(txt);
                sendArg.SetBuffer(buffer, 0, buffer.Length);
                // 发送完成后的回调  
                sendArg.Completed += (objSender, mArg) =>
                {
                    // 如果操作成功  
                    if (mArg.SocketError == SocketError.Success)
                    {
                       // Dispatcher.BeginInvoke(() => txtbInfo.Text = "发送成功。");
                        byte[] data=new byte[1024];
                        mySocket.Receive(data);
                        MessageBox.Show(data.ToString());
                    }
                    else
                    {
                        //Dispatcher.BeginInvoke(() =>
                        //{
                        //    this.txtbInfo.Text = "发送失败，错误：" + mArg.SocketError.ToString();
                        //});
                    }
                    // 报告异步操作结束  
                    MyEvent.Set();
                };
                // 重置信号  
                MyEvent.Reset();
                //txtbInfo.Text = "正在发送，请等候……";
                // 异步发送  
                mySocket.SendAsync(sendArg);
                // 等待操作完成  
                MyEvent.WaitOne(6000);
            }
        }  

    }
    class UserInfo
    {
        public string name { set; get; }
        public string password { set; get; }
        public string ip { set; get; }
    }
}
